(function(factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);

    } else {
        factory(window.jQuery);
    }
}(function($) {

    var css = './starpic/style.css';
    var fun=null;
    try{
        fun = require[require.toUrl ? 'toUrl' : 'resolve'];
    }catch (err) { }
    if (fun) {
        css = fun(css);
        css = '<link rel="stylesheet" href="' + css + '" />';
        if ($('base')[0]) {
            $('base').before(css);
        } else {
            $('head').append(css);
        }
    }

    $.fn.studyplay_star = function(options, callback) {
        //默认设置
        var defaults = {
            MaxStar: 5,
            StarWidth: 26,
            CurrentStar: 0,
            Enabled: true,
            Half: 0
        };
        return this.each(function() {
            var opts = $.extend({}, defaults, options);
            var t=$(this);
            var container = $(this);
            container.css({"position": "relative"})
                    .html('<ul class="studyplay_starBg"></ul>')
                    .find('.studyplay_starBg').width(opts.MaxStar * opts.StarWidth)
                    .html('<li class="studyplay_current studyplay_starovering" style="width:' + opts.CurrentStar * opts.StarWidth + 'px; z-index:0;"></li>');
            if (opts.Enabled) {
                var ListArray = "";
                if (opts.Half == 0){
                    for (k = 1; k < opts.MaxStar + 1; k++){
                        ListArray += '<li class="studyplay_starON" style="width:' + opts.StarWidth * k + 'px;z-index:' + (opts.MaxStar * 2 - k + 1) + ';"></li>';
                    }
                }

                if (opts.Half == 1){
                    for (k = 1; k < opts.MaxStar * 2 + 1; k++){
                        ListArray += '<li class="studyplay_starON" style="width:' + opts.StarWidth * k / 2 + 'px;z-index:' + (opts.MaxStar * 2 - k + 1) + ';"></li>';
                    }
                }
                var scount=0;
                container.find('.studyplay_starBg').append(ListArray);
                container.find('.studyplay_starON').hover(function() {
                    $(this).removeClass('studyplay_starON').addClass("studyplay_starovering");
                    container.find(".studyplay_current").hide();

                    var studyplay_count = opts.MaxStar * 2 - $(this).css("z-index") + 1;
                    if (opts.Half == 0) {
                        container.find(".studyplay_current").width(studyplay_count * opts.StarWidth);
                    }
                    if (opts.Half == 1) {
                        container.find(".studyplay_current").width(studyplay_count * opts.StarWidth / 2);
                    }
                    //回调函数
                    if (typeof callback == 'function') {
                        if (opts.Half == 0)
                            callback(t,studyplay_count * 2);
                        if (opts.Half == 1)
                            callback(t,studyplay_count);
                        return;
                    }
                }, function() {
                    $(this).removeClass('studyplay_starovering').addClass("studyplay_starON");
                    container.find(".studyplay_current").show();
                }).click(function() {
                    var studyplay_count = opts.MaxStar * 2 - $(this).css("z-index") + 1;
                    if (opts.Half == 0) {
                        container.find(".studyplay_current").width(studyplay_count * opts.StarWidth);
                    }
                    if (opts.Half == 1) {
                        container.find(".studyplay_current").width(studyplay_count * opts.StarWidth / 2);
                    }
                    //回调函数
                    if (typeof callback == 'function') {
                        if (opts.Half == 0)
                            callback(t,studyplay_count * 2);
                        if (opts.Half == 1)
                            callback(t,studyplay_count);
                        return;
                    }
                })
            }
        });
    }
}));